package com.example.hdwx.myapplication.HomeActivity.found_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.hdwx.myapplication.R;
import com.example.hdwx.myapplication.adapter.TheFoundAdapter;
import com.example.hdwx.myapplication.eneity.Api;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_everyday_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_hot_eneity;
import com.example.hdwx.myapplication.eneity.daydaycook_eneitys.Daydaycook_raiders_eneity;
import com.example.hdwx.myapplication.eneity.found_eneitys.FoundActivityEneity;
import com.example.hdwx.myapplication.eneity.found_eneitys.FoundCarouselEneity;
import com.example.hdwx.myapplication.eneity.found_eneitys.FoundShareEneity;
import com.example.hdwx.myapplication.eneity.found_eneitys.FoundTopicEneity;
import com.example.hdwx.myapplication.eneity.goodChoice_eneitys.SmallFragmentsEneity;
import com.example.hdwx.myapplication.json.found_json.FoundActivityJSON;
import com.example.hdwx.myapplication.json.found_json.FoundCarouselJSON;
import com.example.hdwx.myapplication.json.found_json.FoundShareJSON;
import com.example.hdwx.myapplication.json.found_json.FoundTopicJSON;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 发现
 */

public class Found_Fragment  extends Fragment {
    RecyclerView recyclerView;
    SwipeRefreshLayout swipeRefreshLayout;
    //活动区
    List<FoundActivityEneity> dataA = new ArrayList<>();
    //话题区
    List<FoundTopicEneity> dataCT = new ArrayList<>();
    //分享区
    List<FoundShareEneity> dataS = new ArrayList<>();
    //轮播图
    List<FoundCarouselEneity> dataCE = new ArrayList<>();

    private TheFoundAdapter adapter;

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.found_fragment, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.found_recycler_view);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.found_swipeRefresh);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新时删除数据库，重新获取数据
                DataSupport.deleteAll(FoundTopicEneity.class);
                DataSupport.deleteAll(FoundShareEneity.class);
                DataSupport.deleteAll(FoundCarouselEneity.class);
                DataSupport.deleteAll(FoundActivityEneity.class);
                getData();
            }
        });
        getData();
    }

    private void getData() {
        //先判断数据库是否有数据，没有就请求数据
        if(dataA.size()!=0||dataCE.size()!=0||dataCT.size()!=0||dataS.size()!=0){
            DataSupport.findAll(FoundActivityEneity.class);
            DataSupport.findAll(FoundTopicEneity.class);
            DataSupport.findAll(FoundShareEneity.class);
            DataSupport.findAll(FoundCarouselEneity.class);
            adapter = new TheFoundAdapter(getContext(), dataA, dataS, dataCT, dataCE);
            recyclerView.setAdapter(adapter);
        }else {
            getData1();
        }


    }
    //活动区
    public void getData1() {
        OkHttpUtils.get().
                url(Api.FOUND_PATH1)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.e("info", "-------活动区下载失败--------");
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("info", "------- 活动区下载成功--------");
                        dataA = FoundActivityJSON.getFoundA(response);

                        Log.e("info", "活动区------" + dataA.size());

                        getData2();
                    }
                });
    }

    public void getData2() {
        //话题区
        OkHttpUtils.get().
                url(Api.FOUND_IMAGE_PATH)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.e("info", "------- 话题区下载失败--------");
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("eeeeeee", "------- 话题区下载成功--------");
                        dataCT = FoundTopicJSON.getFoundCT(response);

                        Log.e("eeeeee", "话题区------" + dataCT.size());
                        getData3();
                    }
                });
    }

    public void getData3() {
        //分享区
        OkHttpUtils.get().
                url(Api.SHARE_PATH)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.e("info", "-------分享区下载失败--------");
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("info", "------- 分享区下载成功--------");
                        dataS = FoundShareJSON.getFoundS(response);

                        Log.e("info", "分享区------" + dataS.size());
                        //适配器
                        getData4();
                    }
                });
    }

    public void getData4() {
        //分享区
        OkHttpUtils.get().
                url(Api.FOUND_IMAGE_PATH)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {
                        Log.e("info", "-------分享区下载失败--------");
                    }

                    @Override
                    public void onResponse(String response) {
                        Log.e("info", "------- 分享区下载成功--------");
                        dataCE = FoundCarouselJSON.getCarouse(response);

                        Log.e("info", "分享区------" + dataCE.size());
                        //适配器
                        adapter = new TheFoundAdapter(getContext(), dataA, dataS, dataCT, dataCE);
                        recyclerView.setAdapter(adapter);
                    }
                });
    }
}
